package org.jcut.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.validation.Valid;

import org.jcut.bean.Address;
import org.jcut.data.Data;
import org.jcut.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/address")
public class AddressController {
	@Autowired
	private IAddressService service;
	@Autowired
	private Data data;
	/**
	 *@author 胡琪
	 *@param id 表id 
	 * */
	@RequestMapping("/delete")
	public Object deleteByPrimaryKey(String id) {
		if(id!=null&&!(id.equals(""))) {
			int i=service.deleteByPrimaryKey(id);
			if(i>0) {
				data.setData("1");
				data.setStatus("1");
				data.setMsg("删除成功");
			}else {
				data.setData("0");
				data.setStatus("0");
				data.setMsg("服务器异常");
			}
		}else {
			data.setData("0");
			data.setStatus("0");
			data.setMsg("用id为空");
		}
		
		return data;
	}
	/**
	 * @author 胡琪
	 * @param uId 用户id
	 * @param provinces 省份
	 * @param city   市
	 * @param distinguish 区
	 * @param street 街道
	 * @param receiver 收货人
	 * @param tel  电话
	 * @param email 邮编
	 * @param street 详细地址
	 * 
	 * 
	 * */
	@RequestMapping("/add")
	public Object insert(@Valid Address address,Errors e) {
		List<ObjectError> error=e.getAllErrors();
		System.out.println(address);
		for(ObjectError ee:error) {
			data.setMsg(ee.getDefaultMessage());			
		}
		if(error.size()==0) {
			address.setId(UUID.randomUUID().toString());
			address.setTime(new Date());
			address.setDis(1);
			int i=service.insert(address);
			if(i>0) {
				data.setData(i);
				data.setMsg("添加成功");
				data.setStatus("1");
			}else {
				data.setData(i);
				data.setMsg("添加失败");
				data.setStatus("0");
			}
		}else {
			data.setData("0");
			data.setStatus("0");
		}
		
    	return data;
    }

   
	/**
	 *@author 胡琪
	 *@param uId 用户id 
	 * */
	@RequestMapping("selectByUid")
	public Object selectByPrimaryKey(@RequestParam("uId")String id) {
		if(id!=null&&!(id.equals(""))) {
			List<Address> i=service.selectByPrimaryKey(id);
			data.setData(i);
			data.setMsg("查询成功");
			data.setStatus("1");
		}else {
			data.setData("0");
			data.setStatus("0");
			data.setMsg("用户id为空");
		}
		return data;
	}
	/**
	 * @author 胡琪
	 * @param uId 用户id
	 * @param provinces 省份
	 * @param city   市
	 * @param distinguish 区
	 * @param street 街道
	 * @param receiver 收货人
	 * @param tel  电话
	 * @param email 邮编
	 * @param 
	 * 
	 * 
	 * */
	@RequestMapping("/update")
	public Object updateByPrimaryKey(@Valid Address address,Errors e) {
		List<ObjectError> error=e.getAllErrors();
		
		for(ObjectError ee:error) {
			data.setMsg(ee.getDefaultMessage());			
		}
		if(error.size()==0) {
			int i=service.updateByPrimaryKey(address);
			if(i>0) {
				data.setData(i);
				data.setMsg("修改成功");
				data.setStatus("1");
			}else {
				data.setData(i);
				data.setMsg("修改失败");
				data.setStatus("0");
			}
		}else {
			data.setData("0");
			data.setStatus("0");
		}
		
    	return data;
	}
	/**
	 * @author 胡琪
	 * @param id 地址id
	 * **/
	@RequestMapping("/selectById")
	public Object selectById(String id) {
		
		Address address=service.selectById(id);
		data.setData(address);
		data.setMsg("查询成功");
		data.setStatus("1");
		return data;
	}
}
